package com.yunxi.service.vdc.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yunxi.core.base.Constant;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.eai.pojo.esb.EsbVO;
import com.yunxi.eai.service.vdc.VDCService;
import com.yunxi.hubToBaseData.service.BaseDataSaveService;
import com.yunxi.hubToBiz.pojo.VdcVehicleCheckVO;
import com.yunxi.hubToBiz.pojo.VehicleDeliveryOrderVO;
import com.yunxi.hubToBiz.service.VdcVehicleCheckService;
import com.yunxi.model.eai.EaiLog;
import com.yunxi.rocketmq.producer.EaiLogsProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by xuruzi on 2019/6/1.
 */
@Service
@Qualifier("vdcServiceImpl")
public class VDCServiceImpl implements VDCService{
    private static final Logger logger = LoggerFactory.getLogger(VDCServiceImpl.class);
    @Autowired
    BaseDataSaveService baseDataSaveServiceByBub;
    @Autowired
    private EaiLogsProducer eaiLogsProducer;
    @Autowired
    private VdcVehicleCheckService vdcVehicleCheckServiceFromBiz;
    @Override
    public ApiResult distributeVDC(EsbVO esbVO) {
        String serviceName = esbVO.getServiceName();
        String reqBody = esbVO.getReqBody();
        ApiResult apiResult=null;
        if (serviceName.equals(Constant.VDC_PMS_VEHICLE_CHECK)) {
            //道闸验证接口
            apiResult=saveVdcVehicleCheck(esbVO);
        } else {
            return ResponseUtil.fail(500, "没有对应的业务接口!");
        }
        return apiResult;
    }


    /**
     * VDC道闸验证接口
     * @param esbVO
     * @return
     */
    private ApiResult saveVdcVehicleCheck(EsbVO esbVO){
        ApiResult apiResult = new ApiResult();
        Map resultMap = new HashMap<>();
        apiResult=ResponseUtil.ok(resultMap);//设置默认返回VDC认证失败
        EaiLog eaiLog=new EaiLog();
        eaiLog.setEsbEaiContent(esbVO.getReqBody());
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("VDC发送车辆或RFID信息给PMS");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(eaiLog.getServiceName());
//        ArrayList<VdcVehicleCheckVO> vdcVehicleCheckList = JSON.parseObject(reqBody, new TypeReference<ArrayList<VdcVehicleCheckVO>>() {
//        });
        VdcVehicleCheckVO vdcVehicleCheckVO = JSON.parseObject(esbVO.getReqBody(),VdcVehicleCheckVO.class);
        try {
            //内部业务
            if(vdcVehicleCheckVO != null ){
                vdcVehicleCheckVO = vdcVehicleCheckServiceFromBiz.vdcVehicleCheck(vdcVehicleCheckVO);
                if(vdcVehicleCheckVO != null){
                    resultMap.put("RoadGateNo",vdcVehicleCheckVO.getRoadGateNo());
                    resultMap.put("PlatesNo",vdcVehicleCheckVO.getPlatesNo());
                    resultMap.put("TransactionID",vdcVehicleCheckVO.getTransactionID());
                    resultMap.put("road",vdcVehicleCheckVO.getRoad());
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    resultMap.put("TimeStamp",sdf.format(new Date()));
                    if (vdcVehicleCheckVO.getStatus()==1){
                        resultMap.put("Type","Y");
                        resultMap.put("status",0);
                        resultMap.put("message","验证成功");
                    }else {
                        resultMap.put("Type","N");
                        resultMap.put("status",-1);
                        resultMap.put("message","验证失败");
                    }
                    //resultMap.put("TimeStamp",vdcVehicleCheckVO.getRoad());
                    apiResult=ResponseUtil.ok(resultMap);
                }
            }
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
        }catch (Exception e){
            apiResult=ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
            logger.error(e.getMessage(),e);
        }
      //  Message message = new Message(rocketConfig.getEaiLogTopic(), "eaiLog",(JSONObject.toJSONString(eaiLog)).getBytes() );
        try {
       //     SendResult send = eaiLogProducer.getProducer().send(message);
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return apiResult;
    }
}
